package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.adapter.HistoryAdapterTing;
import com.example.dilidili_ting.adapter.MovielistAdapterTing;
import com.example.dilidili_ting.bean.ChangUserInfoBeanTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.bean.loginResultBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MovielistActivityTing extends AppCompatActivity {

    private String type_ting;
    private GridView movieListTing;
    private LayoutInflater InflaterTing;
    private RelativeLayout llBarTing;
    private ImageView ivBackTing;
    private TextView tvTitleTing;
    private MovielistAdapterTing movielistAdapterTing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movielist_ting);
//        获取从首页传递的电影类型
        type_ting = getIntent().getStringExtra("type");
        initView();
        getData();
    }

//    初始化控件
    private void initView() {
        llBarTing = (RelativeLayout) findViewById(R.id.ll_bar_ting);
        ivBackTing = (ImageView) findViewById(R.id.iv_back_ting);
        tvTitleTing = (TextView) findViewById(R.id.tv_title_ting);
        movieListTing = (GridView) findViewById(R.id.movie_list_ting);
        tvTitleTing.setText(type_ting+"电影");
        ivBackTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MovielistActivityTing.this.finish();
            }
        });
    }



    //获取电影接口的数据
    private void getData(){
        JsonObject jo = new JsonObject();
        jo.addProperty("type",type_ting);
        OkhttpUtilsTing.isPOST("movie/getMovies", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) { }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<List<MovieBeanTing>> r = gson.fromJson(res, new TypeToken<DataBeanTing<List<MovieBeanTing>>>(){}.getType());
                MovielistActivityTing.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        movielistAdapterTing = new MovielistAdapterTing(MovielistActivityTing.this, r.data);
                        System.out.println("数据"+r.data);
                        Log.i("数据",res);
//                        HistoryAdapterTing historyAdapterTing = new HistoryAdapterTing(MovielistActivityTing.this,r.data);
                        movieListTing.setAdapter(movielistAdapterTing);
                    }
                });
            }
        });
    }

}